/**
 * Created by dale on 2017/9/12.
 */
var typeList;
var prodList;
var currentType;
$(document).ready(function (){
    typeList= $('#typeList');
    prodList=$('#prodList');
    $('.xh-datagrid-toolbar').parseToolbar();
    var typeEvent = new DataGridEvent();
    var prodEvent = new DataGridEvent();

    typeList.initDataGrid(baseUrl+'ims/product/getType','',typeEvent)
        .setSelectThenEnable(['bt_type_del'],[],'eu',typeEvent);



    prodList.initDataGrid(baseUrl+'ims/product/getProduct','',prodEvent)
        .setSelectThenEnable(['bt_prod_del'],[],'eu',prodEvent);


    // xhTool.bindDocKeyDown($('#bt_list_search'));
    $('#bt_prod_add').click(function () {
        window.top.xhTop.showModal({
            title:'新增商品',
            content: '<iframe src="'+baseUrl+'ims/product/prodForm?iTypeId='+currentType+'"  style="width: 100%;;border-style: none;padding: 0px;height:220px"  scrolling="yes"   ></iframe>',
            button:'提交',
            backClose: false,
        });
    });

    $('#bt_type_add').click(function () {
        window.top.xhTop.showModal({
            title:'新增类型',
            content: '<iframe src="'+baseUrl+'ims/product/typeForm"  style="width: 100%;;border-style: none;padding: 0px;height: 60px"  scrolling="yes"   ></iframe>',
            button:'提交',
            backClose: false,
        });
    });
    typeEvent.onSelect=function(rowIndex, rowData){
        console.log('aaa');
        currentType=rowData['iTypeId'];
        prodList.addDataGridParams({iTypeId:rowData['iTypeId']}).datagrid('getPanel').panel('setTitle', '商品列表（' + rowData['sTypeName'] + '）');
    };

    // typeEvent.onUnselect=function(rowIndex, rowData){
    //     console.log(rowIndex)
    //     currentType='';
    //     prodList.addDataGridParams({iTypeId:0}).datagrid('getPanel').panel('setTitle', '商品列表（全部）');
    // };


    typeList.datagrid({
        onCheck:function (rowIndex,rowData) {
            $('#bt_type_del').setButtonEnable('eu');
        },
        onUncheck:function (rowIndex,rowData) {
            var _rows = typeList.datagrid('getChecked');
            if(_rows.length==0){
                $('#bt_type_del').setButtonDisable('eu');
            }
        },
        onBeforeSelect:function (rowIndex,rowData) {
            console.log('before');
            if(currentType==rowData['iTypeId']){
                currentType='';
                prodList.addDataGridParams({iTypeId:0}).datagrid('getPanel').panel('setTitle', '商品列表（全部）');
                typeList.datagrid('clearSelections');
                return false;
            }

        }
    })


    typeEvent.onDblClickRow=function(rowIndex, rowData){
            window.top.xhTop.showModal({
                title:'编辑类型',
                content: '<iframe src="'+baseUrl+'ims/product/typeForm?id='+rowData['iTypeId']+'"  style="width: 100%;;border-style: none;padding: 0px;height: 60px"  scrolling="yes"   ></iframe>',
                button:'提交',
                backClose: false,
            });
    };

    prodEvent.onDblClickRow=function (rowIndex, rowData) {
        window.top.xhTop.showModal({
            title:'编辑商品',
            content: '<iframe src="'+baseUrl+'ims/product/prodForm?id='+rowData['iProdId']+'"  style="width: 100%;;border-style: none;padding: 0px;height: 220px"  scrolling="yes"   ></iframe>',
            button:'提交',
            backClose: false,
        });
    };

    //
    // $('#bt_type_see').click(function () {
    //     if($(this).hasClass('l-btn-selected')){
    //         $(this).linkbutton('unselect');
    //     }else {
    //         $(this).linkbutton('select');
    //     }
    //
    // });





    $('#bt_prod_del').click(function () {
        var ids=prodList.getDataGridSelectedFields('iProdId');
        myWindow.initPopUp({title: '确定要删除么?',text: '这些商品将被删除!'},function () {
            $.ajax({
                url:baseUrl+'ims/product/delProd',
                data:{ids:ids},
                type:'POST',
                success:function (data) {
                    window.top.swal(
                        data,
                        '',
                        'success'
                    );
                    prodList.dataGridReload();
                },
                error:function (data) {
                    window.top.swal(
                        data['responseText'],
                        '',
                        'success'
                    );
                    prodList.dataGridReload();
                }
            });
        });
        
    });

    $('#bt_type_del').click(function () {
        var ids=getDataGridChekedFields('iTypeId');
        myWindow.initPopUp({title: '确定要删除么?', text: '该类别存在商品!'},function () {
            $.ajax({
                url:baseUrl+'ims/product/delType',
                data:{ids:ids},
                type:'POST',
                success:function (data) {
                    window.top.swal(
                        data,
                        '',
                        'success'
                    );
                    typeList.dataGridReload();
                },
                error:function (data) {
                    window.top.swal(
                        data['responseText'],
                        '',
                        'success'
                    );
                }
            });
        });
    });

    function getDataGridChekedFields(_field, _array){
        var _rows = typeList.datagrid('getChecked');
        var _ids = [];
        var _tmp;
        for(var i=0; i<_rows.length; i++){
            _tmp = eval('_rows['+i+'].'+_field);
            // if(_tmp){
            _ids.push(_tmp);
            // }
        }
        return _array ? _ids : _ids.join(',');
    };
    
    

});